<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of newPHPClass
 *
 * @author Administrator
 */
class PlayerController extends Controller {

    public function actionIndex() {
        $model = new Player();
        $this->model = $model;
        $this->performAjaxValidation($model);
        if (Yii::app()->request->getIsPostRequest()) {
            $model->attributes = Yii::app()->request->getPost('Player');
            if ($model->save()) {
                $this->redirect($this->createUrl('player/index'));
            }
        }
        $this->render('index', array('model' => $model));
    }

    public function actionEdit() {
        $model = new Player();
        $this->performAjaxValidation($model);
        $id = (int) Yii::app()->request->getParam('id');
        $modify = $model->findByPk($id);
        if (Yii::app()->request->getIsPostRequest()) {
            $modify->attributes = Yii::app()->request->getPost('Player');
            if ($modify->save()) {
                $this->redirect($this->createUrl('player/index'));
            }
        }
        $this->render('edit', array('modify' => $modify));
    }

    public function actionDel() {
        $model = new Player();
        $this->performAjaxValidation($model);
        $id = (int) Yii::app()->request->getParam('id');
        $modify = $model->findByPk($id);
        if ($modify ) {
            if( $modify->delete()){
                $this->redirect($this->createUrl('player/index'));
            }else{
                $error = $modify->getError('player_delete_failed');
            }
        }else{
            $error = $modify->getError('player_not_exists');
        }
        $this->render("/layouts/message", array('message' => $error));
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'player-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}

?>
